package BeenChina.PM.Action; 

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import BeenChina.PM.Model.ModelUser;
import BeenChina.PM.Service.ServiceFactory; 
import BeenChina.PM.Util.CookieUtil; 
import BeenChina.PM.Util.SecurityUtil;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;  
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport
{
	private ModelUser user ;
	private String msg;
	private String  savePass ="on";
	
	private static final Log log = LogFactory.getLog(LoginAction.class);
	  	  
	public ModelUser getUser(){
		return user;
	}
	
	public void setUser(ModelUser value){
		user=value;		
	}
	
	public String loginCheck(){
		boolean loginvalid=	ServiceFactory.getInstance().getSystemInfoService().verifyUser(user.getUserID(), user.getUserPass());
		if (loginvalid){
			return "valid";
		}
		else{
			msg="ERROR";
			return Action.ERROR;
		}
	}
	
	@Override
	public String execute(){						
		msg="error";
		if (this.user==null){
			HttpServletRequest request = ServletActionContext.getRequest();
			ModelUser cookieUser =  CookieUtil.getCookie(request);
			boolean flag= loginFromCookie(cookieUser);
			if (flag){
				msg="OK";
			}	
		}
		else if (user!=null){
			boolean flag= loginFromPage(user);
			if (flag){
				msg="OK";
			}		  
		}
		
		return msg=="OK" ? Action.SUCCESS : Action.LOGIN;
	}
	

	public String loginFromCookie(){
		//cookie is true 
		// is valid
		msg="ERROR";		
		return Action.LOGIN;		 
	}
		
	public String logout(){
		try{
			CookieUtil.delCookie();
			return Action.LOGIN;
		}
		catch(Exception exp){
			//TODO not implement exception in struts.xml
			return "exception";
		}
	}
	
	private boolean loginFromPage(ModelUser user){
		
		if (user !=null){
			log.info("login user:" + user.getUserID());
		}
		else {
			return false;
		}
		CookieUtil.delCookie();
		 
		boolean loginvalid=	ServiceFactory.getInstance().getSystemInfoService().verifyUser(user.getUserID(), user.getUserPass());
		
		if(loginvalid) {
			UUID uuid = UUID.randomUUID();
			user.setSessionId(uuid.toString());
 		
			Calendar cal = Calendar.getInstance();
			cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
			user.setLastActionTime( sdf.format(cal.getTime()) );
			
			//save to db
			ServiceFactory.getInstance().getSystemInfoService().updateSessionId(user);
			
			if(this.savePass.equals("on")){
				CookieUtil.setCookie(user);
			}
			
			ActionContext context = ActionContext.getContext();	
			context.getSession().put("user", user);				
			
			return true;
		}
		return false;
	}
 
	private boolean loginFromCookie(ModelUser cookieUser) {
		
		if (cookieUser !=null){
			log.info("login user:" + cookieUser.getUserID());
		}
		else {
			return false;
		}
		ModelUser dbUser = ServiceFactory.getInstance().getSystemInfoService().selectUser(cookieUser.getUserID());
		boolean loginvalid=	SecurityUtil.authenticatePassword(cookieUser.getUserPass(), dbUser.getUserPass());
		
	 	 
		if(loginvalid) {
			UUID uuid = UUID.randomUUID();
			dbUser.setSessionId(uuid.toString());
 		
			Calendar cal = Calendar.getInstance();
			cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
			dbUser.setLastActionTime( sdf.format(cal.getTime()) );
			
			//save to db
			ServiceFactory.getInstance().getSystemInfoService().updateSessionId(dbUser);
			
			if(this.savePass.equals("on")){
				//	save to cookie
				CookieUtil.setCookie(dbUser);
			}
						
			ActionContext context = ActionContext.getContext();	
			context.getSession().put("user", dbUser);				
			
			return true;
		}
		return false;
	}
	
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String isSavePass() {
		return savePass;
	}

	public void setSavePass(String  savePass) {
		this.savePass = savePass;
	}
	
}
